package com.younion.action.consumer.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IFavourableService;
import com.younion.business.IFavourableSmsCheckService;
import com.younion.business.IRegisterUserService;
import com.younion.business.ISendMsgService;
import com.younion.common.constants.MsgStatus;
import com.younion.model.FavourableCheckResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.CustomerVO;
import com.younion.vo.FavourableSmsCheckVO;
import com.younion.vo.FavourableVO;
import com.younion.vo.RegisterUserVO;
import com.younion.vo.SendMsgVO;

public class FavourableCheckCodeGetConsumer extends AbstractRequestConsumer {
	private IFavourableSmsCheckService favourableSmsCheckService;
	private IRegisterUserService registerUserService;
	private ISendMsgService sendMsgService;
	private IFavourableService favourableService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getCode(request, response);
	}

	public String getCode(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		String fav_id = request.getParameter("fav_id");
		ResultObject resultObject = new ResultObject();
		FavourableCheckResult result = new FavourableCheckResult();
		try {
			Map paramMap = new HashMap();
			paramMap.put("userToken", user_token);
			RegisterUserVO registerUserVO = registerUserService
					.getVOByMap(paramMap);
			FavourableVO favourableVO = favourableService.getById(fav_id);
			if (registerUserVO != null) {
				FavourableSmsCheckVO favourableSmsCheckVO = new FavourableSmsCheckVO();
				List<FavourableSmsCheckVO> favourableSmsCheckVOs = (List<FavourableSmsCheckVO>) favourableSmsCheckService
						.getByMap(paramMap);
				if (favourableSmsCheckVOs != null
						&& !favourableSmsCheckVOs.isEmpty()) {
					favourableSmsCheckVO = favourableSmsCheckVOs.get(0);
				}
				if(favourableVO != null){
					String code = getRandom(4);
					favourableSmsCheckVO.setCode(code);
					favourableSmsCheckVO.setLastTime(new Date());
					favourableSmsCheckVO.setMobile(registerUserVO.getLoginId());
					favourableSmsCheckVO
							.setUserToken(registerUserVO.getUserToken());
					SendMsgVO sendMsgVO = new SendMsgVO();
					sendMsgVO.setSendTime(new Date());
					sendMsgVO.setStatus(MsgStatus.UNSEND.getName());
					sendMsgVO.setMobile(registerUserVO.getLoginId());
					sendMsgVO.setContent("您使用优惠券的验证码是：" + code);
					sendMsgVO.setUsers(favourableVO.getMemberCard() == null ? "" : favourableVO.getMemberCard().toString());
					sendMsgVO.setTitle("优惠券验证码");
					CustomerVO customerVO = new CustomerVO();
					customerVO.setId(favourableVO.getCustomerId());
					sendMsgVO.setCustomer(customerVO);
					sendMsgService.save(sendMsgVO);
					favourableSmsCheckService.save(favourableSmsCheckVO);
					resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
					result.setSuccessful("1");	
				} else{
					resultObject.setStatus(ApiConstant.FALSE_STATUS);
					result.setSuccessful("0");
					resultObject.setMsg("优惠券不存在");
				}
			} else {
				resultObject.setStatus(ApiConstant.FALSE_STATUS);
				result.setSuccessful("0");
				resultObject.setMsg("用户不存在");
			}
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			result.setSuccessful("0");
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		resultObject.setResult(result);
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	private String getRandom(int num) {
		java.util.Random r = new java.util.Random();
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < num; i++) {
			result.append(r.nextInt(10));
		}
		return result.toString();
	}

	public void setFavourableSmsCheckService(
			IFavourableSmsCheckService favourableSmsCheckService) {
		this.favourableSmsCheckService = favourableSmsCheckService;
	}

	public void setRegisterUserService(IRegisterUserService registerUserService) {
		this.registerUserService = registerUserService;
	}

	public void setSendMsgService(ISendMsgService sendMsgService) {
		this.sendMsgService = sendMsgService;
	}

	public void setFavourableService(IFavourableService favourableService) {
		this.favourableService = favourableService;
	}
}
